package com.meiqiongproperty.yiguanjia.fragment.home.life;

import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.activity.GenericFragmentActivity;
import com.meiqiongproperty.yiguanjia.databinding.YiLifeMainLayoutBinding;
import com.meiqiongproperty.yiguanjia.db.utils.DBUserUtils;
import com.meiqiongproperty.yiguanjia.fragment.BaseFragment;
import com.meiqiongproperty.yiguanjia.http.api.ApiService;
import com.meiqiongproperty.yiguanjia.http.interfaces.IHttpCallback;
import com.meiqiongproperty.yiguanjia.http.sendJsonModel.SendAdvertId;
import com.meiqiongproperty.yiguanjia.httpbean.AdvertInfoBean;
import com.meiqiongproperty.yiguanjia.ui.adbanner.animations.DescriptionAnimation;
import com.meiqiongproperty.yiguanjia.ui.adbanner.indicators.PagerIndicator;
import com.meiqiongproperty.yiguanjia.ui.adbanner.slidertypes.BaseSliderView;
import com.meiqiongproperty.yiguanjia.ui.adbanner.slidertypes.TextSliderView;
import com.meiqiongproperty.yiguanjia.ui.adbanner.viewpagerswitchanimation.SliderLayout;
import com.meiqiongproperty.yiguanjia.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者: yangbin
 * 时间: 2017/5/5
 * 包名: com.meiqiongproperty.yiguanjia.fragment.home.life
 * 用途：怡生活主界面
 */
public class YiLifeFragment extends BaseFragment {
    YiLifeMainLayoutBinding binding;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.yi_life_main_layout, container, false);
        binding.setOnclick(this);
        binding.navigation.setLeftClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendKeyBackEvent();
            }
        });
        binding.navigation.setTitle("怡生活");
        getData();
        return binding.getRoot();
    }

    /**
     * 更多点击
     *
     * @param view
     */
    public void liftMore(View view) {
        GenericFragmentActivity.start(getActivity(), LifeMoreFragment.class, new Bundle());
    }

    /**
     * 私人定制
     *
     * @param view
     */
    public void personalTailorClick(View view) {
        Bundle bundle = new Bundle();
        bundle.putString("title", "私人定制");
        GenericFragmentActivity.start(getActivity(), HouseKeepServiceFragment.class, bundle);
    }

    /**
     * 家政服务
     *
     * @param view
     */
    public void housekeepClick(View view) {
        Bundle bundle = new Bundle();
        bundle.putString("cateId", "1");
        bundle.putString("title", "家政服务");
        GenericFragmentActivity.start(getActivity(), HouseKeepServiceFragment.class, bundle);
    }

    /**
     * 居家维修
     */
    public void homeMaintainClick(View view) {
        Bundle bundle = new Bundle();
        bundle.putString("cateId", "2");
        bundle.putString("title", "居家维修");
        GenericFragmentActivity.start(getActivity(), HouseKeepServiceFragment.class, bundle);
    }


    //获取广告
    public void getData() {
        SendAdvertId send = new SendAdvertId();
        send.setAdvertId("2");
        send.setToken(DBUserUtils.getUserInfo(getActivity()).getToken());
        ApiService.getAdvertInfo(getActivity(), new IHttpCallback<AdvertInfoBean>() {
            @Override
            public void onNext(AdvertInfoBean advertInfoBean) {
                initAdv(advertInfoBean);
            }

            @Override
            public void onError(int code, String message) {
                Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
            }
        }, Utils.getRequestBody(send));
    }

    public void initAdv(final AdvertInfoBean advertInfoBean) {
        if (isAdded()) {
            binding.slider.getPagerIndicator().setDefaultIndicatorColor(getActivity().getResources().getColor(R.color.transparent), Color.argb(1, 255, 255, 255));
        }
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                binding.slider.stopAutoCycle();
            }
        }, 200);

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (advertInfoBean.getAdvert().size() <= 1) {
                    binding.slider.setBottomIndicator(View.GONE);
                    binding.slider.getPagerIndicator().setIndicatorVisibility(PagerIndicator.IndicatorVisibility.Invisible);
                } else {
                    binding.slider.setBottomIndicator(View.VISIBLE);
                    binding.slider.getPagerIndicator().setIndicatorVisibility(PagerIndicator.IndicatorVisibility.Visible);
                    if (isAdded()) {
                        binding.slider.getPagerIndicator().setDefaultIndicatorColor(getActivity().getResources().getColor(R.color.life_cycler_point), Color.argb(66, 255, 255, 255));
                    }
                }

                if (advertInfoBean.getAdvert().size() <= 1) {
                    binding.slider.stopAutoCycle();
                } else {
                    binding.slider.startAutoCycle();
                }
            }
        }, advertInfoBean.getAdvert().size() * 1000);

        List<String> img = new ArrayList<String>();
        List<String> imgurl = new ArrayList<String>();
        List<String> title = new ArrayList<String>();
        for (int i = 0; i < advertInfoBean.getAdvert().size(); i++) {
            img.add(i, advertInfoBean.getAdvert().get(i).getImage());
            imgurl.add(i, advertInfoBean.getAdvert().get(i).getUrl());
            title.add(i, advertInfoBean.getAdvert().get(i).getTitle());
        }
        TextSliderView textSliderView = new TextSliderView(getActivity(), img, imgurl, title);

        for (int i = 0; i < advertInfoBean.getAdvert().size(); i++) {
            // initialize a SliderLayout
            textSliderView
                    .image(img.get(i))
                    .setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
                        @Override
                        public void onSliderClick(BaseSliderView slider) {

                        }
                    });

            binding.slider.addSlider(textSliderView);
        }
        binding.slider.setPresetTransformer(SliderLayout.Transformer.Stack);
        binding.slider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
        binding.slider.setCustomAnimation(new DescriptionAnimation());
    }
}
